//
// Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
//
// GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
//  Author: Shawn.Tan
//  Date : 2025.10.28
//
//  History : Initial Version 2025.10.28
//
//
#include "RecoveryAddressGenerator.h"

#include <memory>

#include "GenRequest.h"
#include "Generator.h"
#include "Log.h"
#include "VaGenerator.h"
#include "VmInfo.h"
#include "VmManager.h"
#include "VmMapper.h"

using namespace std;

/*!
  \file RecoveryAddressGenerator.cc
  \brief Code supporting generating addresses to use for recovery operations such as in fault handlers or register reloading.
*/

namespace Green {

  RecoveryAddressGenerator::RecoveryAddressGenerator(const Generator* pGenerator)
    : Object(), mpGenerator(pGenerator)
  {
  }

  uint64 RecoveryAddressGenerator::GenerateAddress(cuint64 align, cuint64 size, cbool isInstr, const EMemAccessType memAccessType) const
  {
    std::unique_ptr<const GenPageRequest> page_req(CreateGenPageRequest(isInstr, memAccessType));
    VmMapper* vm_mapper = GetCurrentVmMapper();
    VaGenerator va_gen(vm_mapper, page_req.get());
    uint64 addr = va_gen.GenerateAddress(align, size, isInstr, memAccessType);

    LOG(notice) << "{RecoveryAddressGenerator::GenerateAddress} generated addr=0x" << hex << addr << endl;

    return addr;
  }

  VmMapper* RecoveryAddressGenerator::GetCurrentVmMapper() const
  {
    VmManager* vm_manager = mpGenerator->GetVmManager();
    return vm_manager->CurrentVmMapper();
  }

}
